package com.vegetable.modules.controller.baseData;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.vegetable.common.annotation.SysLog;
import com.vegetable.common.utils.PageUtils;
import com.vegetable.common.utils.R;
import com.vegetable.modules.controller.AbstractController;
import com.vegetable.modules.entity.baseData.AddressBook;
import com.vegetable.modules.entity.baseData.BaseDict;
import com.vegetable.modules.entity.baseData.po.AddressBookQueryDTO;
import com.vegetable.modules.service.baseData.IAddressBookService;
import com.vegetable.modules.service.baseData.IBaseDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * <p>
 * 通讯录（农户,客商,代办,班组） 前端控制器
 * </p>
 *
 * @author wangruijie
 * @since 2024-03-07
 */
@Slf4j
@RestController
@RequestMapping("/addressBook")
@AllArgsConstructor
@Api(value = "通讯录（农户,客商,代办,班组）", tags = "通讯录（农户,客商,代办,班组）")
public class AddressBookController extends AbstractController {
    private IBaseDictService baseDictService;
    private IAddressBookService addressBookService;
    /**
     * 所有通讯录列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "条件查询通讯录列表", notes = "条件查询通讯录列表")
    public R list(AddressBookQueryDTO queryDTO){
        if (ObjectUtil.isNull(queryDTO.getStaffType())){
            return R.fail("通讯录类型不能为空");
        }
        log.info("条件查询通讯录列表{}", JSON.toJSONString(queryDTO));
        PageUtils page = addressBookService.queryPage(queryDTO);

        return R.data(page);
    }

    /**
     * 新增通讯录
     */
    @SysLog(value = "新增通讯录")
    @PostMapping("/save")
    @ApiOperation(value = "新增通讯录", notes = "新增通讯录")
    public R save(@RequestBody AddressBook addressBook){
        AddressBook alias = addressBookService.queryByAliseName(addressBook.getAlias(), addressBook.getStaffType());
        AddressBook note = addressBookService.queryByAliseName(addressBook.getNoteName(), addressBook.getStaffType());
        if(Objects.nonNull(alias)){
            throw new RuntimeException("代号:" + addressBook.getAlias() + "已经存在了，请重新起名字");
        }
        if(Objects.nonNull(note)){
            throw new RuntimeException("名称:" + addressBook.getNoteName() + "已经存在了，请重新起名字");
        }
        if(StrUtil.isBlank(addressBook.getAlias())){
            addressBook.setAlias(addressBook.getNoteName());
        }

        addressBook.setCreateUserId(getUserId());
        addressBookService.save(addressBook);
        return R.status(true);
    }

    /**
     * 修改通讯录
     */
    @SysLog(value = "修改通讯录")
    @PostMapping("/update")
    @ApiOperation(value = "修改通讯录", notes = "修改通讯录")
    public R update(@RequestBody AddressBook addressBook){
        AddressBook alias = addressBookService.queryByAliseName(addressBook.getAlias(), addressBook.getStaffType());
        AddressBook note = addressBookService.queryByAliseName(addressBook.getNoteName(), addressBook.getStaffType());
        if(Objects.nonNull(alias) && !Objects.equals(alias.getId(), addressBook.getId())){
            throw new RuntimeException("代号:" + addressBook.getAlias() + "已经存在了，请重新起名字");
        }
        if(Objects.nonNull(note) && !Objects.equals(note.getId(), addressBook.getId())){
            throw new RuntimeException("名称:" + addressBook.getNoteName() + "已经存在了，请重新起名字");
        }
        addressBook.setCreateUserId(getUserId());
        addressBookService.updateById(addressBook);
        return R.status(true);
    }

    /**
     * 删除通讯录
     */
    @SysLog(value = "删除通讯录")
    @GetMapping("/delete")
    @ApiOperation(value = "删除通讯录", notes = "删除通讯录")
    public R delete(Long id){
        AddressBook addressBook = addressBookService.getById(id);
        if (addressBook == null){
            return R.fail("数据不存在");
        }
        addressBook.setIsDel(1);
        addressBook.setCreateUserId(getUserId());
        addressBookService.updateById(addressBook);
        return R.status(true);
    }

    @SysLog(value = "更新初始欠款")
    @GetMapping("/updateInitArrears")
    @ApiOperation(value = "更新初始欠款", notes = "更新初始欠款")
    public R updateInitArrears(Long id, BigDecimal initArrears){
        AddressBook addressBook = addressBookService.getById(id);
        if (addressBook == null){
            return R.fail("数据不存在");
        }
        AddressBook updater = new AddressBook();
        updater.setId(id);
        updater.setInitArrears(initArrears);
        addressBookService.updateById(updater);
        return R.success("");
    }
}